﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Backend.JYD.BLL;
using Backend.JYD.RunTime;
using System.Web.Security;
using Backend.JYD.Model;

namespace Backend.JYD.Web.Controllers
{
    public class HomeController : AbController
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            this.ViewBag.Msg = string.Empty;
            this.ViewBag.Name = string.Empty;
            this.ViewBag.Pwd = string.Empty;
            return View();
        }
        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            this.ViewBag.Msg = string.Empty;
            //var info = this.ControllerContext.RequestContext.HttpContext.Session;
            string name = collection["name"];
            string pwd = collection["pwd"];
            if (string.IsNullOrWhiteSpace(name))
            {
                this.ViewBag.Msg = "请输入登录名！";
            }
            else if (string.IsNullOrWhiteSpace(pwd))
            {
                this.ViewBag.Msg = "请输入密码！";
            }
            else
            {
                var info = new EmpInfoBLL().GetLoginEmpInfo(name, pwd);
                if (info != null)
                {
                    if (info.Status < 1)
                    {
                        this.ViewBag.Msg = "该账户未开通，请联系管理员！";
                    }
                    else
                    {
                        SessionMgr.ResetSession(this.HttpContext, info);
                        FormsAuthentication.SetAuthCookie(info.EmpId.ToString(), false);
                        //LoginMgr.doLogin(info);
                        return RedirectToAction("mine");
                    }

                }
                else
                {
                    this.ViewBag.Msg = "用户名不存在或密码错误！";
                }
            }
            return View();
        }

        public ActionResult LoginOut()
        {
            SessionMgr.ClearSeession(this.HttpContext);

            return RedirectToAction("index");
        }

        public ActionResult Mine()
        {
            return View();
        }

        public ActionResult Test()
        {
            List<TreeNodeInfo> nodes = new List<TreeNodeInfo>();
            nodes.Add(new TreeNodeInfo(1, "父节点1", null));
            nodes.Add(new TreeNodeInfo(11, "节点1->子节点11", 1, string.Empty, false, false));
            nodes.Add(new TreeNodeInfo(111, "节点1->节点11->子节点111", 11, string.Empty, true, false));
            nodes.Add(new TreeNodeInfo(1111, "节点1->节点11->节点111->子节点1111", 111, string.Empty, false, true));
            nodes.Add(new TreeNodeInfo(1112, "节点1->节点11->节点111->子节点1112", 111, string.Empty, false, true));
            nodes.Add(new TreeNodeInfo(112, "节点1->节点11->子节点112", 11, string.Empty, false, false));
            nodes.Add(new TreeNodeInfo(1121, "节点1->节点11->节点112->子节点1121", 112, string.Empty, false, false));
            nodes.Add(new TreeNodeInfo(11211, "节点1->节点11->节点112->子节点1121->子节点11211", 1121, string.Empty, false, true));
            nodes.Add(new TreeNodeInfo(11212, "节点1->节点11->节点112->子节点1121->子节点11212", 1121, string.Empty, true, true));
            nodes.Add(new TreeNodeInfo(1122, "节点1->节点11->节点112->子节点1122", 112, string.Empty, false, true));

            nodes.Add(new TreeNodeInfo(12, "节点1->子节点12", 1, string.Empty, false, false));
            nodes.Add(new TreeNodeInfo(121, "节点1->节点12->子节点121", 12, string.Empty, false, false));
            nodes.Add(new TreeNodeInfo(1211, "节点1->节点12->节点121->子节点1211", 121, string.Empty, false, true));
            nodes.Add(new TreeNodeInfo(1212, "节点1->节点12->节点121->子节点1212", 121, string.Empty, false, true));
            nodes.Add(new TreeNodeInfo(122, "节点1->节点12->子节点122", 12, string.Empty, false, false));
            nodes.Add(new TreeNodeInfo(1221, "节点1->节点12->节点122->子节点1221", 122, string.Empty, false, true));
            nodes.Add(new TreeNodeInfo(1222, "节点1->节点12->节点122->子节点1222", 122, string.Empty, false, true));

            nodes.Add(new TreeNodeInfo(2, "父节点2", null));
            nodes.Add(new TreeNodeInfo(21, "节点2->子节点21", 2, string.Empty, false, false));
            nodes.Add(new TreeNodeInfo(211, "节点2->节点21->子节点211", 21, string.Empty, false, false));
            nodes.Add(new TreeNodeInfo(2111, "节点2->节点21->节点211->子节点2111", 211, string.Empty, false, true));
            nodes.Add(new TreeNodeInfo(2112, "节点2->节点21->节点211->子节点2112", 211, string.Empty, false, true));
            nodes.Add(new TreeNodeInfo(212, "节点2->节点21->子节点212", 21, string.Empty, false, false));
            nodes.Add(new TreeNodeInfo(2121, "节点2->节点21->节点212->子节点2121", 212, string.Empty, false, true));
            nodes.Add(new TreeNodeInfo(2122, "节点2->节点21->节点212->子节点2122", 212, string.Empty, false, true));

            this.ViewBag.TreeNodes = nodes;
            return View();
        }
    }
}
